package org.example;

/**
 * 二分查找
 */
public class BinarySearchSolution {

    /**
     * 二分查找算法
     * @param arr   有序数组
     * @param start 开始下标
     * @param end   结束下标
     * @param value 值
     * @return
     */
    public boolean binarySearch(int[] arr, int start, int end, int value) {
        if (start == end) {
            return arr[start] == value;
        }
        int mid = (start + end) / 2;
        if (arr[mid] >= value) {
            return binarySearch(arr, start, mid, value);
        }
        return binarySearch(arr, mid + 1, end, value);
    }

    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 6, 7, 8, 9, 10, 11, 12};
        int value = 5;
        BinarySearchSolution solution = new BinarySearchSolution();
        System.out.println(solution.binarySearch(arr, 0, arr.length - 1, value));
    }
}
